import axios from "axios";
import {notification,Modal} from "ant-design-vue";
import store from "../store";
import types from "../store/types";
import VueCookies from "vue-cookies";

let instance = axios.create({
  timeout: 60000
});

axios.defaults.baseURL = "http://10.9.238.234:8050/";

//拦截请求
instance.interceptors.request.use(
  config => {
    // store.commit(types.LOADING, true);
    let url = config.url;
    if(url.indexOf("?") > -1){
      const list = url.split("?");
      url = list[0];
    }
    store.commit("loading", url);
    return config;
  },
  error => {
    // store.commit(types.LOADING, false);
    return Promise.reject(error);
  }
);

let flag = true;

//拦截响应
instance.interceptors.response.use(
  res => {
    let url = res.config.url;
    if(url.indexOf("?") > -1){
      const list = url.split("?");
      url = list[0];
    }
    store.commit("unloading", url.replace(axios.defaults.baseURL, ""));
    // store.commit("LOADING", false);
    if (res && res.data) {
      if (res.data.success === true) {
        return res.data.result;
      } else {
        if (res.data.errorCode === '401') {
          if(flag){
            flag = false;
            Modal.confirm({
              title: '尚未登录或登录超时!!!',
              content: '用户未登录或登录超时,请选择继续使用或重新登录？',
              okText: '登录以继续使用',
              cancelText: '重新登录',
              onOk() {
                flag = true;
                let userInfo = VueCookies.get("userInfo");
                userInfo.continueFlag = true;
                if (userInfo) {
                  store.dispatch("userStore/setUserInfo", userInfo);
                }
              },
              onCancel() {
                flag = true;
                VueCookies.remove("userInfo");
                window.location.reload();
              },
            });
          }
          // notification.error({
          //   message: '尚未登录',
          //   description: '用户未登录或登录超时，即将跳转到登录界面',
          //   duration: 3,
          //   onClose: () => {
          //     VueCookies.remove("userInfo");
          //     window.location.reload();
          //     // this.$router.push("/login");
          //   }
          // });
          //登陆超时前台已做处理，不需要再进行后端异常提醒
          // return Promise.reject(new Error(res.data.errorMsg));
        } else {
          notification.error({
            message: '错误通知',
            description: res.data.resume + (res.data.result ? res.data.result : "")
          });
          return Promise.reject(new Error(res.data.resume + (res.data.result ? res.data.result : "")));
        }
      }
    }
  },
  error => {
    store.commit(types.LOADING, false);
    notification.error({
      message: '网络异常',
      description: error.toString()
    });
    return Promise.reject(error);
  }
);

//请求post方式
const fetchPost = function (params) {
  return new Promise((resolve, reject) => {
    instance
      .post(params.url, params.data)
      .then(res => {
        resolve(res);
      })
      .catch(error => {
        reject(error);
      });
  });
};

//请求get方式
const fetchGet = function (params) {
  return new Promise((resolve, reject) => {
    instance
      .get(params.url)
      .then(res => {
        resolve(res);
      })
      .catch(error => {
        reject(error);
      });
  });
};

export default {fetchGet, fetchPost};
